package settings_api

import (
	"github.com/gin-gonic/gin"
	"zswei.xyz/manager/config"
	"zswei.xyz/manager/core"
	"zswei.xyz/manager/global"
	"zswei.xyz/manager/models/res"
)

// SettingsUpdateView 修改系统配置文件
//	@Tags			系统管理
//	@Summary		修改系统配置文件
//	@Description	修改系统配置文件
//	@Param			data	query	string	true	"site-系统配置 email-邮箱配置 qq-qq配置 qiniu-七牛配置 jwt-jwt权限配置 upload-上传文件配置"
//	@Router			/v2/system/settings [put]
//	@Produce		json
//	@Success		200	{object}	res.Response{}
func (SettingsApi) SettingsUpdateView(c *gin.Context) {
	setType := c.Query("type")
	var err error

	if setType == "" {
		res.FailWithDesc("Type is a required parameter", c)
		return
	}

	switch setType {
	case "site":
		var info config.SiteInfo
		err = c.ShouldBindJSON(&info)
		if err != nil {
			res.FailWithCode(res.ArgumentError, c)
			return
		}
		global.Config.SiteInfo = info
	case "email":
		var info config.Email
		err = c.ShouldBindJSON(&info)
		if err != nil {
			res.FailWithCode(res.ArgumentError, c)
			return
		}
		global.Config.Email = info
	case "qq":
		var info config.QQ
		err = c.ShouldBindJSON(&info)
		if err != nil {
			res.FailWithCode(res.ArgumentError, c)
			return
		}
		global.Config.QQ = info
	case "qiniu":
		var info config.QiNiu
		err = c.ShouldBindJSON(&info)
		if err != nil {
			res.FailWithCode(res.ArgumentError, c)
			return
		}
		global.Config.QiNiu = info
	case "jwt":
		var info config.Jwt
		err = c.ShouldBindJSON(&info)
		if err != nil {
			res.FailWithCode(res.ArgumentError, c)
			return
		}
		global.Config.Jwt = info

	case "upload":
		var info config.Upload
		err = c.ShouldBindJSON(&info)
		if err != nil {
			res.FailWithCode(res.ArgumentError, c)
			return
		}
		global.Config.Upload = info

	default:
		res.FailWithDesc("未查找到相关配置", c)
		return
	}

	core.SetYaml()
	res.OkWith(c)
}
